package lk.icta.common.entity;

import lk.icta.common.AppConstants;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(schema = "COMMON_RAIL")
@NamedQueries({
        @NamedQuery(name = "get.gnDivisions.by.code", query = "SELECT gn FROM GNDivision gn WHERE " +
                "(gn.code =:gnDivisionCode AND gn.dsDivision =:dsDivision)"),
        @NamedQuery(name = "get.all.gnDivisions.by.dsDivisionId", query = "SELECT gn FROM GNDivision gn WHERE " +
                "gn.dsDivision.id = :dsDivisionId"),
        @NamedQuery(name = "get.gnDivision.by.name.dsDivision", query = "SELECT gn FROM GNDivision gn " +
                "WHERE gn.dsDivision.id = :dsDivision " +
                "AND (gn.siDivisionName = :siName OR gn.enDivisionName = :enName OR gn.taDivisionName = :taName)")
})
public class GNDivision implements Serializable {
    /**
     * A system generated unique key
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    /**
     * code of the gn division
     */
    @Column(updatable = false, nullable = false)
    private int code;

    /**
     * The relationship to D.S. Division of which this G.N. Division belongs
     */
    @ManyToOne
    @JoinColumn
    private DSDivision dsDivision;

    @Column(nullable = false, updatable = false)
    private String siDivisionName;
    @Column(nullable = false, updatable = false)
    private String enDivisionName;
    @Column(nullable = false, updatable = false)
    private String taDivisionName;

    @Column(columnDefinition = "smallint not null default 1")
    private boolean active;

    public GNDivision() {
    }

    public GNDivision(DSDivision dsDivision,
                      String siDivisionName, String enDivisionName, String taDivisionName, boolean active) {
        this.dsDivision = dsDivision;
        this.siDivisionName = siDivisionName;
        this.enDivisionName = enDivisionName;
        this.taDivisionName = taDivisionName;
        this.active = active;
    }

    public DSDivision getDsDivision() {
        return dsDivision;
    }

    public void setDsDivision(DSDivision dsDivision) {
        this.dsDivision = dsDivision;
    }

    public String getSiDivisionName() {
        return siDivisionName;
    }

    public void setSiDivisionName(String siDivisionName) {
        this.siDivisionName = siDivisionName;
    }

    public String getEnDivisionName() {
        return enDivisionName;
    }

    public void setEnDivisionName(String enDivisionName) {
        this.enDivisionName = enDivisionName;
    }

    public String getTaDivisionName() {
        return taDivisionName;
    }

    public void setTaDivisionName(String taDivisionName) {
        this.taDivisionName = taDivisionName;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public int getDistrictId() {
        return dsDivision.getDistrictId();
    }

    public void setDistrictId(int districtId) {
        this.dsDivision.setDistrictId(districtId);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getGNDivisionNameByLanguage(String language) {
        String divisionName = "";
        if (AppConstants.ENGLISH.equals(language)) {
            divisionName = getEnDivisionName();
        } else if (AppConstants.SINHALA.equals(language)) {
            divisionName = getSiDivisionName();
        } else if (AppConstants.TAMIL.equals(language)) {
            divisionName = getTaDivisionName();
        }

        return divisionName;
    }
}